﻿using System.Modules.SharePoint.ExceptionHanlding;
using System.Workflow.Activities;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Workflow;

namespace System.Modules.SharePoint.Workflow
{
    /// <summary>
    /// Base Sequential Workflow Activity
    /// </summary>
    public class BaseSequentialWorkflowActivity : SequentialWorkflowActivity
    {
        /// <summary>
        /// Workflow Id
        /// </summary>
        public virtual Guid WorkflowId { get { return Guid.Empty; } }

        /// <summary>
        /// Workflow Properties
        /// </summary>
        public virtual SPWorkflowActivationProperties WorkflowProperties
        {
            get { return null; }
        }

        /// <summary>
        /// Write To History Log
        /// </summary>
        /// <param name="eventType"></param>
        public void WriteToHistoryLog(SPWorkflowHistoryEventType eventType)
        {
            if (WorkflowProperties != null)
            {
                WriteToHistoryLog(eventType, WorkflowProperties.OriginatorUser, string.Empty, string.Empty, string.Empty);
            }
        }

        /// <summary>
        /// Write To History Log
        /// </summary>
        /// <param name="eventType"></param>
        /// <param name="description"></param>
        public void WriteToHistoryLog(SPWorkflowHistoryEventType eventType, string description)
        {
            if (WorkflowProperties != null)
            {
                WriteToHistoryLog(eventType, WorkflowProperties.OriginatorUser, string.Empty, description, string.Empty);
            }
        }

        /// <summary>
        /// Write To History Log
        /// </summary>
        /// <param name="eventType"></param>
        /// <param name="outcome"></param>
        /// <param name="description"></param>
        public void WriteToHistoryLog(SPWorkflowHistoryEventType eventType, string outcome, string description)
        {
            if (WorkflowProperties != null)
            {
                WriteToHistoryLog(eventType, WorkflowProperties.OriginatorUser, outcome, description, string.Empty);
            }
        }

        /// <summary>
        /// Write To History Log
        /// </summary>
        /// <param name="eventType"></param>
        /// <param name="outcome"></param>
        /// <param name="description"></param>
        /// <param name="otherData"></param>
        public void WriteToHistoryLog(SPWorkflowHistoryEventType eventType, string outcome, string description, string otherData)
        {
            if (WorkflowProperties != null)
            {
                WriteToHistoryLog(eventType, WorkflowProperties.OriginatorUser, outcome, description, otherData);
            }
        }

        /// <summary>
        /// Write To History Log
        /// </summary>
        /// <param name="eventType"></param>
        /// <param name="user"></param>
        public void WriteToHistoryLog(SPWorkflowHistoryEventType eventType, SPMember user)
        {
            if (WorkflowProperties != null && WorkflowProperties.Web != null)
            {
                WriteToHistoryLog(WorkflowProperties.Web, WorkflowId, eventType, user, new TimeSpan(), string.Empty, string.Empty, string.Empty);
            }
        }

        /// <summary>
        /// Write To History Log
        /// </summary>
        /// <param name="eventType"></param>
        /// <param name="user"></param>
        /// <param name="description"></param>
        public void WriteToHistoryLog(SPWorkflowHistoryEventType eventType, SPMember user, string description)
        {
            if (WorkflowProperties != null && WorkflowProperties.Web != null)
            {
                WriteToHistoryLog(WorkflowProperties.Web, WorkflowId, eventType, user, new TimeSpan(), string.Empty, description, string.Empty);
            }
        }

        /// <summary>
        /// Write To History Log
        /// </summary>
        /// <param name="eventType"></param>
        /// <param name="user"></param>
        /// <param name="outcome"></param>
        /// <param name="description"></param>
        public void WriteToHistoryLog(SPWorkflowHistoryEventType eventType, SPMember user, string outcome, string description)
        {
            if (WorkflowProperties != null && WorkflowProperties.Web != null)
            {
                WriteToHistoryLog(WorkflowProperties.Web, WorkflowId, eventType, user, new TimeSpan(), outcome, description, string.Empty);
            }
        }

        /// <summary>
        /// Write To History Log
        /// </summary>
        /// <param name="eventType"></param>
        /// <param name="user"></param>
        /// <param name="outcome"></param>
        /// <param name="description"></param>
        /// <param name="otherData"></param>
        public void WriteToHistoryLog(SPWorkflowHistoryEventType eventType, SPMember user, string outcome, string description, string otherData)
        {
            if (WorkflowProperties != null && WorkflowProperties.Web != null)
            {
                WriteToHistoryLog(WorkflowProperties.Web, WorkflowId, eventType, user, new TimeSpan(), outcome, description, otherData);
            }
        }

        /// <summary>
        /// Write To History Log
        /// </summary>
        /// <param name="web"></param>
        /// <param name="workflowId"></param>
        /// <param name="eventType"></param>
        /// <param name="user"></param>
        /// <param name="duration"></param>
        /// <param name="outcome"></param>
        /// <param name="description"></param>
        /// <param name="otherData"></param>
        public void WriteToHistoryLog(SPWeb web, Guid workflowId, SPWorkflowHistoryEventType eventType, SPMember user, TimeSpan duration, string outcome, string description, string otherData)
        {
            SPWorkflow.CreateHistoryEvent(web, workflowId, (int)eventType, user, duration, outcome, description, otherData);
        }

        /// <summary>
        /// Handles Workflow Exception
        /// </summary>
        public Exception WorkflowException
        {
            set
            {
                if (value != null)
                {
                    value.Handle();
                    WriteToHistoryLog(SPWorkflowHistoryEventType.WorkflowError, value.StackTrace, value.Message, string.Empty);
                }
            }
        }
    }
}